package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceSallaryTemplateDAO;
import org.jsola.hr.dto.AttendanceSallaryTemplateAddDTO;
import org.jsola.hr.dto.AttendanceSallaryTemplateUpdateDTO;
import org.jsola.hr.entity.AttendanceSallaryTemplateDO;
import org.jsola.hr.query.AttendanceSallaryTemplateQuery;
import org.jsola.hr.service.IAttendanceSallaryTemplateService;
import org.jsola.hr.vo.AttendanceSallaryTemplateListVO;
import org.jsola.hr.vo.AttendanceSallaryTemplateVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤工资模板表
 *
 * @author zhr
 */
@Service("hrAttendanceSallaryTemplateServiceImpl")
public class AttendanceSallaryTemplateServiceImpl implements IAttendanceSallaryTemplateService {

    @Autowired
    private IAttendanceSallaryTemplateDAO attendanceSallaryTemplateDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceSallaryTemplateVO save(AttendanceSallaryTemplateAddDTO attendanceSallaryTemplateAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceSallaryTemplateDO attendanceSallaryTemplateDO = attendanceSallaryTemplateAddDTO.to(AttendanceSallaryTemplateDO.class);
        // 保存
        attendanceSallaryTemplateDO = save(attendanceSallaryTemplateDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendanceSallaryTemplateDO.to(AttendanceSallaryTemplateVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceSallaryTemplateUpdateDTO attendanceSallaryTemplateUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceSallaryTemplateDO attendanceSallaryTemplateDO = attendanceSallaryTemplateUpdateDTO.to(AttendanceSallaryTemplateDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceSallaryTemplateDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceSallaryTemplateIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceSallaryTemplateIds);
    }


    @Override
    public AttendanceSallaryTemplateVO selectById(String attendanceSallaryTemplateId, String siteId) {
        AttendanceSallaryTemplateDO attendanceSallaryTemplateDO = selectDOById(attendanceSallaryTemplateId, siteId);
        if (attendanceSallaryTemplateDO == null) {
            return null;
        }
        return attendanceSallaryTemplateDO.to(AttendanceSallaryTemplateVO.class);
    }

    @Override
    public List<AttendanceSallaryTemplateListVO> select(AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery, String siteId) {
        List<AttendanceSallaryTemplateDO> attendanceSallaryTemplateDOList = selectDO(attendanceSallaryTemplateQuery, siteId);
        if(CollectionUtils.isEmpty(attendanceSallaryTemplateDOList)) {
            return attendanceSallaryTemplateDOList == null ? null : new ArrayList<>();
        }
        return attendanceSallaryTemplateDOList.stream()
                .map(attendanceSallaryTemplateDO -> attendanceSallaryTemplateDO.to(AttendanceSallaryTemplateListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery, String siteId) {
        Example example = buildExample(attendanceSallaryTemplateQuery,siteId);
        return attendanceSallaryTemplateDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceSallaryTemplateListVO> selectPage(AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery, String siteId) {
        Example example = buildExample(attendanceSallaryTemplateQuery,siteId);
        Page<AttendanceSallaryTemplateDO> page = attendanceSallaryTemplateDAO.selectPageByExample(example,
                attendanceSallaryTemplateQuery.getPageNo(),
                attendanceSallaryTemplateQuery.getPageSize());

        return page.to(AttendanceSallaryTemplateListVO.class);
    }

    @Override
    public AttendanceSallaryTemplateDO selectDOById(String attendanceSallaryTemplateId, String siteId) {
        return listById(attendanceSallaryTemplateId, siteId);
    }

    @Override
    public List<AttendanceSallaryTemplateDO> selectDO(AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery, String siteId) {
        Example example = buildExample(attendanceSallaryTemplateQuery,siteId);
        return attendanceSallaryTemplateDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendanceSallaryTemplateQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery, String siteId) {
        Example example = new Example(AttendanceSallaryTemplateDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        if(!StringUtils.isEmpty(attendanceSallaryTemplateQuery.getCompanyId())){
            example.and().andEqualTo("companyId",attendanceSallaryTemplateQuery.getCompanyId());
        }
        ExampleKit.setExampleOrder(example,attendanceSallaryTemplateQuery.getOrders());
        return example;
    }
}




